<template>
  <div class="app-container">
    <div class="filter-container" v-show="!delete1">
      <el-input
        v-model="listQuery.nickname"
        placeholder="姓名"
        style="width: 200px"
        class="filter-item"
        clearable
        @keyup.enter.native="handleFilter"
        @clear="handleFilter"
        @blur="handleFilter"
      />
      <el-button
        v-waves
        class="filter-item"
        type="primary"
        icon="el-icon-search"
        style="margin-left: 10px"
        @click="handleFilter"
      >
        查询
      </el-button>
      <el-button
        class="filter-item"
        type="primary"
        icon="el-icon-edit"
        style="margin-left: 5px"
        @click="handleCreate"
      >
        新增
      </el-button>
    </div>
    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%"
      :default-sort="defaultSort"
      @sort-change="sortChange"
    >
      <el-table-column
        label="ID"
        prop="id"
        sortable="custom"
        align="center"
        width="80"
      />

      <el-table-column label="用户名" prop="username" width="150" align="center" />
      <el-table-column label="角色" prop="role" width="100" align="center" />
      <el-table-column label="真实姓名" prop="nickname" width="100" align="center" />
      <el-table-column label="邮箱" prop="email" align="center" />
      <el-table-column label="联系电话" prop="contactNumber" width="100" align="center" />
      <el-table-column label="注册时间" prop="registTime" width="100" align="center">
        <template slot-scope="{ row: { registTime }}">
          <span>{{ registTime | timeFilter }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        width="200"
        align="center"
        fixed="right"
      >
        <template slot-scope="{ row }">
          <el-button type="text" icon="el-icon-edit" title ='修改密用户信息' @click="handleUpdate(row)" >修改</el-button>
          <el-button type="text" icon="el-icon-view" title ='修改密码' @click="handlePasswordUpdate(row)" >密码</el-button>
          <el-button type="text" icon="el-icon-delete" style="color:#f56c6c" title ='删除用户信息' @click="handleDelete(row)" v-show="!delete1" >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.pageSize"
      @pagination="refresh"
    />
  </div>
</template>

<script>
// eslint-disable-next-line
/* eslint-disable */
import Pagination from '../../components/Pagination/index'
import waves from '../../directive/waves/waves'
import { listUser, deleteUser } from '../../api/user'
import { parseTime } from '../../utils'
import Cookies from 'js-cookie'

//import moment from 'moment'
//Vue.prototype.$moment = moment
export default {
  components: { Pagination },
  directives: { waves },
  filters: {
    valueFilter(value) {
      return value || '无'
    },
    timeFilter(time) {
      return time ? parseTime(time, '{y}-{m}-{d} {h}:{i}') : '无'
    }
  },
  data() {
    return {
      tableKey: 0,
      listLoading: true,
      listQuery: {},
      showCover: false,
      categoryList: [],
      list: [],
      total: 0,
      defaultSort: {},
      baoming:'',
      delete1:false
    }
  },
  
  created() {
    this.parseQuery()
  },
  mounted() {
    this.getList()
    this.getCategoryList()
  },
  beforeRouteUpdate(to, from, next) {
    if (to.path === from.path) {
      const newQuery = Object.assign({}, to.query)
      const oldQuery = Object.assign({}, from.query)
      if (JSON.stringify(newQuery) !== JSON.stringify(oldQuery)) {
        this.getList()
      }
    }
    next()
  },
  methods: {
    parseQuery() {
      const query = Object.assign({}, this.$route.query)
      let sort = '+id'
      const listQuery = {
        page: 1,
        pageSize: 20,
        sort
      }
      if (query) {
        query.page && (query.page = +query.page)
        query.pageSize && (query.pageSize = +query.pageSize)
        query.sort && (sort = query.sort)
      }
      const sortSymbol = sort[0]
      const sortColumn = sort.slice(1, sort.length)
      ////console.log(sortSymbol, sortColumn)
      this.defaultSort = {
        prop: sortColumn,
        order: sortSymbol === '+' ? 'ascending' : 'descending'
      }
      this.listQuery = { ...listQuery, ...query }
    },
    sortChange(data) {
      ////console.log('sortChange', data)
      const { prop, order } = data
      this.sortBy(prop, order)
    },
    sortBy(prop, order) {
      if (order === 'ascending') {
        this.listQuery.sort = `+${prop}`
      } else {
        this.listQuery.sort = `-${prop}`
      }
      this.handleFilter()
    },
    wrapperKeyword(k, v) {
      function highlight(value) {
        return `<span style="color: #1890ff">${value}</span>`
      }

      if (!this.listQuery[k]) {
        return v
      } else {
        return v.replace(new RegExp(this.listQuery[k], 'ig'), v => highlight(v))
      }
    },
    getList() {
      this.listLoading = true
      //let roleinfo=this.roles
      let usernameinfo=Cookies.get('username')
      if(usernameinfo!='admin'){
        this.listQuery.username=usernameinfo
        this.delete1=true
      }
      listUser(this.listQuery).then(response => {
        ////console.log(response)
        const { list, count } = response.data
        this.list = list
        //////console.log(list.photo)
        this.total = count
        this.listLoading = false
      })
    },
    getCategoryList() {
     
    },
    refresh() {
      this.$router.push({
        path: '/user/list',
        query: this.listQuery
      })
    },
    handleFilter() {
      ////console.log('handleFilter', this.listQuery)
      this.listQuery.page = 1
      this.refresh()
    },
    handleCreate() {
      /* this.baoming=this.roles
      ////console.log(this.roles)
      if(this.baoming=='admin'){
        this.$router.push('/enroll/create')
      } else if(this.list.length!=0){
        this.$notify({
          title: '你已经填写过了报名信息，不能重复报名',
          message: '报名失败',
          type: 'error',
          duration: 2000
        }) 
      }else{
        this.$router.push('/enroll/create')
      } */
      this.$router.push('/user/create')
      
    },
    handleUpdate(row) {
      //////console.log('handleUpdate', row)
      this.$router.push(`/user/edit/${row.id}`)
    },
    handlePasswordUpdate(row) {
      //////console.log('handleUpdate', row)
      this.$router.push(`/user/password/${row.id}`)
    },
    handleDelete(row) {
      let usernameinfo=Cookies.get('username')
      if(usernameinfo!='admin'){
        this.$notify({
            title: '失败',
            message: response.msg || '不能删除用户',
            type: 'error',
            duration: 2000
          })
      }else{
        this.$confirm('此操作将永久删除该用户，是否继续？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteUser(row.id).then(response => {
          this.$notify({
            title: '成功',
            message: response.msg || '删除成功',
            type: 'success',
            duration: 2000
          })
          this.handleFilter()
        })
      })
      }
    },
    changeShowCover(value) {
      this.showCover = value
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
